﻿
    using Chain.BLL;
//    using ChainStock.Controls;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Wuqi.Webdiyer;

    public partial class GoodsLog : PageBase
    {


        private void BingChangeType()
        {
            this.sltChangeType.Items.Add(new ListItem("===== 请选择 =====", "-1"));
            this.sltChangeType.Items.Add(new ListItem("商品入库", "1"));
            this.sltChangeType.Items.Add(new ListItem("商品销售出库", "2"));
            this.sltChangeType.Items.Add(new ListItem("商品挂单出库", "3"));
            this.sltChangeType.Items.Add(new ListItem("撤销订单入库", "4"));
            this.sltChangeType.Items.Add(new ListItem("商品撤单入库", "5"));
            this.sltChangeType.Items.Add(new ListItem("商品退货入库", "7"));
            this.sltChangeType.Items.Add(new ListItem("商品编辑库存入库", "8"));
            this.sltChangeType.Items.Add(new ListItem("商品编辑库存出库", "9"));
            this.sltChangeType.Items.Add(new ListItem("商品入库撤单", "10"));
            this.sltChangeType.Items.Add(new ListItem("商品入库(已撤单)", "11"));
            this.sltChangeType.Items.Add(new ListItem("商品调拨出库", "12"));
            this.sltChangeType.Items.Add(new ListItem("商品调拨入库", "13"));
            this.sltChangeType.Items.Add(new ListItem("商品库存批量导入", "14"));
        }

        protected void btnGoodsLogQuery_Click(object sender, EventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = 1;
            this.Get_ParameterList(this.QueryCondition());
        }

        protected void btnRptExpenseExcel_Click(object sender, EventArgs e)
        {
            Chain.BLL.GoodsLog bllGoodsLog = new Chain.BLL.GoodsLog();
            int Counts = this.NetPagerParameter.RecordCount;
            string strSql = this.QueryCondition();
            string strAgent = "";
            strSql = strSql + " and GoodsLog.ShopID>0" + " and GoodsLog.ShopID = SysShop.ShopID and GoodsLog.UserID = SysUser.UserID";
            strSql = PubFunction.GetShopAuthority(base._UserShopID, "GoodsLog.ShopID", strSql);
            DataTable db = bllGoodsLog.GetListSP(0x186a0, 1, out Counts, new string[] { strSql }).Tables[0];
            DataExcelInfo.GoodsLogExcel(db, base._UserName, strAgent);
        }

        protected void drpPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = 1;
            this.NetPagerParameter.PageSize = int.Parse(this.drpPageSize.SelectedValue);
            this.Get_ParameterList(this.QueryCondition());
        }

        private void Get_ParameterList(string strSql)
        {
            Chain.BLL.GoodsLog bllGoodsLog = new Chain.BLL.GoodsLog();
            int Counts = this.NetPagerParameter.RecordCount;
            strSql = strSql + " and GoodsLog.ShopID>0";
            strSql = strSql + " and GoodsLog.ShopID = SysShop.ShopID and GoodsLog.UserID = SysUser.UserID";
            strSql = PubFunction.GetShopAuthority(base._UserShopID, "GoodsLog.ShopID", strSql);
            DataTable db = bllGoodsLog.GetListSP(this.NetPagerParameter.PageSize, this.NetPagerParameter.CurrentPageIndex, out Counts, new string[] { strSql }).Tables[0];
            this.NetPagerParameter.RecordCount = Counts;
            this.NetPagerParameter.CustomInfoHTML = string.Format("<div class=\"results\"><span>当前第{0}/{1}页 共{2}条记录 每页{3}条</span></div>", new object[] { this.NetPagerParameter.CurrentPageIndex, this.NetPagerParameter.PageCount, this.NetPagerParameter.RecordCount, this.NetPagerParameter.PageSize });
            this.rptGoodsLog.DataSource = db;
            this.rptGoodsLog.DataBind();
            PageBase.BindSerialRepeater(this.rptGoodsLog, this.NetPagerParameter.PageSize * (this.NetPagerParameter.CurrentPageIndex - 1));
        }

        protected string GetType(int Type)
        {
            string strType = "";
            switch (Type)
            {
                case 1:
                    return "商品入库";

                case 2:
                    return "商品销售出库";

                case 3:
                    return "商品挂单出库";

                case 4:
                    return "撤销订单入库";

                case 5:
                    return "商品撤单入库";

                case 6:
                    return strType;

                case 7:
                    return "商品退货入库";

                case 8:
                    return "商品编辑库存入库";

                case 9:
                    return "商品编辑库存出库";

                case 10:
                    return "商品入库撤单";

                case 11:
                    return "商品入库(已撤单)";

                case 12:
                    return "商品调拨出库";

                case 13:
                    return "商品调拨入库";

                case 14:
                    return "商品库存批量导入";
            }
            return strType;
        }

        protected void NetPagerParameter_PageChanging(object src, PageChangingEventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = e.NewPageIndex;
            this.Get_ParameterList(this.QueryCondition());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                PubFunction.BindUserSelect(base._UserShopID, this.sltUserID, true, false);
                PubFunction.BindShopSelect(base._UserShopID, this.sltShop, base._UserShopID, base._UserShopID != 1);
                this.BingChangeType();
                this.Get_ParameterList(this.QueryCondition());
                this.txtUser.Value = base._UserName;
                PubFunction.Get_PrintTitle(ref this.lblPrintTitle, ref this.lblPrintFoot, base._UserShopID);
            }
            this.PointNum.Value = PubFunction.GetPointNum("CKRKML");
        }

        protected string QueryCondition()
        {
            string strQuery = PubFunction.RemoveSpace(this.txtQuery.Value);
            string strUserlID = this.sltUserID.Value;
            string strShopID = this.sltShop.Value;
            int intChangeType = int.Parse(this.sltChangeType.Value);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("1=1");
            if (strQuery != "")
            {
                strSql.AppendFormat(" and ((GoodsAccount like '%{0}%') or (id in( select goodslogid from goodslogdetail where \r\n                                    goodsid in(select goodsid from goods where name like '%{0}%'))))", strQuery);
            }
            if (strShopID != "")
            {
                strSql.AppendFormat(" and (GoodsLog.ShopID={0} or ChangeShopID={0})", int.Parse(strShopID));
            }
            if (strUserlID != "")
            {
                strSql.AppendFormat(" and GoodsLog.UserID={0}", int.Parse(strUserlID));
            }
            if (this.txtStartTime.Value != "")
            {
                strSql.AppendFormat(" and CreateTime>='{0}' ", this.txtStartTime.Value);
            }
            if (this.txtEndTime.Value != "")
            {
                try
                {
                    DateTime txtEndTimes = DateTime.Parse(Convert.ToDateTime(this.txtEndTime.Value).ToString("yyyy-MM-dd") + " 23:59:59");
                    strSql.AppendFormat(" and CreateTime<='{0}'", txtEndTimes);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            if (intChangeType != -1)
            {
                strSql.AppendFormat(" and Type = {0}", intChangeType);
            }
            return strSql.ToString();
        }

        protected void rptGoodsLog_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                DataRowView dr = (DataRowView) e.Item.DataItem;
                Repeater rptItem = (Repeater) e.Item.FindControl("rptGoodsLogDetail");
                if (rptItem != null)
                {
                    int recordCount = this.NetPagerParameter.RecordCount;
                    string strSql = "  GoodsLogDetail.GoodsID=Goods.GoodsID ";
                    strSql = strSql + " and GoodsLogDetail.GoodsLogID=" + dr["ID"];
                    GoodsLogDetail bllGoodsLogDetail = new GoodsLogDetail();
                    DataTable dtDetail = bllGoodsLogDetail.GetListSP(strSql).Tables[0];
                    rptItem.DataSource = dtDetail;
                    rptItem.DataBind();
                    foreach (RepeaterItem rp in rptItem.Items)
                    {
                        Label lblDetailNum = (Label) rp.FindControl("lblDetailNumber");
                        lblDetailNum.Text = (rp.ItemIndex + 1).ToString();
                    }
                }
            }
        }
    }


